package org.sanguine.study.hometasks.first.task.a;

import java.io.BufferedReader;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Scanner;

public abstract class CommonWordTask extends CommonStringTask {

	@Override
	protected void readItems() {
		BufferedReader reader = new BufferedReader(in);
		int countOfItems = 0;
		Scanner scanner = new Scanner(reader);
		String inString;
		
		for (int i = 0; i < numberOfItems; i++) {
			try {
				inString = scanner.next();
			} catch (NoSuchElementException | IllegalStateException e) {
				System.err.println("Unexpected end of stream");
				break;
			}
			items[countOfItems++] = inString;
		}
		scanner.close();
		
		if (countOfItems == 0) items = null;
		else if (countOfItems < numberOfItems) {
			items = Arrays.copyOf(items, countOfItems);
		}

	}

	protected int countDifferentChars(String word) {
		if (word.isEmpty()) return 0;
		
		char[] charSet = word.toCharArray();
		Arrays.sort(charSet);
		int count = 1;
		char prev = charSet[0];
		for (int i = 1; i < charSet.length; i++) {
			if (charSet[i] != prev) {
				count++;
				prev = charSet[i];
			}
		}
	
		return count;
	}

}
